#!/bin/bash
# 清理旧的日志文件脚本

# 默认保留天数（可通过参数修改）
DAYS_TO_KEEP=${1:-7}

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 获取脚本所在目录的父目录（项目根目录）
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

echo -e "${YELLOW}开始清理超过 ${DAYS_TO_KEEP} 天的日志文件...${NC}"
echo -e "${BLUE}项目根目录: ${PROJECT_ROOT}${NC}"

# 清理计数器
DELETED_COUNT=0

# 清理webhook日志
if [ -d "$PROJECT_ROOT/logs/webhooks" ]; then
  echo -e "\n${GREEN}清理 webhook 日志:${NC}"
  while IFS= read -r file; do
    echo "  删除: $file"
    rm -f "$file"
    ((DELETED_COUNT++))
  done < <(find "$PROJECT_ROOT/logs/webhooks" -name "*.json" -type f -mtime +$DAYS_TO_KEEP)
fi

# 清理评审日志（.log文件）
if [ -d "$PROJECT_ROOT/logs/reviews" ]; then
  echo -e "\n${GREEN}清理评审日志:${NC}"
  while IFS= read -r file; do
    echo "  删除: $file"
    rm -f "$file"
    ((DELETED_COUNT++))
  done < <(find "$PROJECT_ROOT/logs/reviews" -name "*.log" -type f -mtime +$DAYS_TO_KEEP)
fi

# 清理评审报告（.txt文件）
if [ -d "$PROJECT_ROOT/logs/reviews" ]; then
  echo -e "\n${GREEN}清理评审报告:${NC}"
  while IFS= read -r file; do
    echo "  删除: $file"
    rm -f "$file"
    ((DELETED_COUNT++))
  done < <(find "$PROJECT_ROOT/logs/reviews" -name "report-*.txt" -type f -mtime +$DAYS_TO_KEEP)
fi

# 清理调试日志
if [ -d "$PROJECT_ROOT/logs/debug" ]; then
  echo -e "\n${GREEN}清理调试日志:${NC}"
  while IFS= read -r file; do
    echo "  删除: $file"
    rm -f "$file"
    ((DELETED_COUNT++))
  done < <(find "$PROJECT_ROOT/logs/debug" -name "*.log" -type f -mtime +$DAYS_TO_KEEP)
fi

# 清理webhook-runner的日志
if [ -d "$PROJECT_ROOT/webhook-runner/logs" ]; then
  echo -e "\n${GREEN}清理 webhook-runner 日志:${NC}"
  while IFS= read -r file; do
    echo "  删除: $file"
    rm -f "$file"
    ((DELETED_COUNT++))
  done < <(find "$PROJECT_ROOT/webhook-runner/logs" -type f -mtime +$DAYS_TO_KEEP)
fi

# 统计剩余文件
if [ -d "$PROJECT_ROOT/logs" ]; then
  echo -e "\n${YELLOW}清理完成！共删除 ${DELETED_COUNT} 个文件${NC}"
  echo -e "\n${BLUE}剩余日志文件统计:${NC}"
  
  # 统计各类型文件数量
  WEBHOOK_COUNT=$(find "$PROJECT_ROOT/logs/webhooks" -name "*.json" 2>/dev/null | wc -l | tr -d ' ')
  REVIEW_LOG_COUNT=$(find "$PROJECT_ROOT/logs/reviews" -name "*.log" 2>/dev/null | wc -l | tr -d ' ')
  REVIEW_REPORT_COUNT=$(find "$PROJECT_ROOT/logs/reviews" -name "report-*.txt" 2>/dev/null | wc -l | tr -d ' ')
  DEBUG_COUNT=$(find "$PROJECT_ROOT/logs/debug" -name "*.log" 2>/dev/null | wc -l | tr -d ' ')
  
  echo -e "  Webhook日志: ${GREEN}${WEBHOOK_COUNT}${NC} 个"
  echo -e "  评审日志: ${GREEN}${REVIEW_LOG_COUNT}${NC} 个"
  echo -e "  评审报告: ${GREEN}${REVIEW_REPORT_COUNT}${NC} 个"
  echo -e "  调试日志: ${GREEN}${DEBUG_COUNT}${NC} 个"
  
  # 显示各目录磁盘占用
  echo -e "\n${BLUE}磁盘占用详情:${NC}"
  if [ -d "$PROJECT_ROOT/logs/webhooks" ]; then
    WEBHOOK_SIZE=$(du -sh "$PROJECT_ROOT/logs/webhooks" 2>/dev/null | cut -f1)
    echo -e "  Webhook目录: ${WEBHOOK_SIZE}"
  fi
  if [ -d "$PROJECT_ROOT/logs/reviews" ]; then
    REVIEW_SIZE=$(du -sh "$PROJECT_ROOT/logs/reviews" 2>/dev/null | cut -f1)
    echo -e "  评审目录: ${REVIEW_SIZE}"
  fi
  if [ -d "$PROJECT_ROOT/logs/debug" ]; then
    DEBUG_SIZE=$(du -sh "$PROJECT_ROOT/logs/debug" 2>/dev/null | cut -f1)
    echo -e "  调试目录: ${DEBUG_SIZE}"
  fi
  
  # 总磁盘占用
  TOTAL_SIZE=$(du -sh "$PROJECT_ROOT/logs" 2>/dev/null | cut -f1)
  echo -e "  ${YELLOW}总占用: ${TOTAL_SIZE}${NC}"
fi

echo -e "\n${GREEN}✅ 日志清理完成${NC}"